//获取应用实例
const app = getApp();

Page({
	data: {
    bannerList: [{}],
    tempBannerList:[{}],
    motto: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    navData: [],
    bkshopList:[],
    currentTab: 0,
    navScrollLeft: 0,
    //准备数据 
    menu: {},
    goodsList:[],
    quanprice:0
  },
	onLoad: function() {
    var that = this;
    that.getTopGoodsList();
    wx.getStorage({
      key: 'token',
      success: function (res) {
        console.log(res)
        if(res.data!=''){
            app.globalData.token=res.data
        }
        console.log("token=" + app.globalData.token)
      },
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })       
  },

  //获取顶级京东商品分类列表
  getTopGoodsList() {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    var navDatas = [{
      icon: '',
      is_show: '',
      name: '爆款',
      pid: '',
      sort: '',
      taobao_cat_id: '',
      tb_cat_id: ''
    }]
    wx.request({
      url: app.globalData.api + '/app.php?c=JingdongCat&a=getTopCatList',
      data: {
        token: app.globalData.token
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
       
        that.setData({
          navData: navDatas.concat(res.data.data.list),
         
        })
        
        that.getTopCatList(0);
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },
  //获取商品列表
  getTopCatList(positions) {
    console.log(positions)
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.apiJd + '/jd/goodslist',
      data: {
        apikey:'f55ab951b3089ae2',
        goodslist:1,
        pagesize:50,
        iscoupon:1,
        sort:'asc',
        minprice:30,
        isunion:1,
        cid1:positions
      },
      header: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if(res.data.data.code==200){
          var priceQuan;
          for(var i=0;i<res.data.data.data.length;i++){
            res.data.data.data[i].priceInfo.price = (res.data.data.data[i].priceInfo.lowestPrice - res.data.data.data[i].couponInfo.couponList[0].discount).toFixed(2)
            res.data.data.data[i].couponInfo.couponList[0].discount = res.data.data.data[i].couponInfo.couponList[0].discount.toFixed(1)
          }
          
          that.setData({
            bkshopList: res.data.data.data,
            // quanprice: priceQuan.toFixed(2)
          })
          // console.log(that.data.quanprice)
        }else{
          wx.showToast({
            title: ''+res.data.message,
            icon:'none'
          })
          that.setData({
            bkshopList:[]
          })
        }
        
      },
      fail: function () {
        wx.hideLoading();
      }
    });
  },

  
  toTbkdetail:function(e){
    console.log(this.data.bkshopList[e.currentTarget.dataset.index])
    app.globalData.jdList = this.data.bkshopList[e.currentTarget.dataset.index]
    wx.navigateTo({
      url: '/pages/shop/jd-item',
    })
  },
  toDetail:function(e){
    console.log(e)
  },
onShow(){

},
  switchNav(event) {
    var cur = event.currentTarget.dataset.current;
    //每个tab选项宽度占1/5
    var singleNavWidth = this.data.windowWidth / 5;
    //tab选项居中                            
    this.setData({
      navScrollLeft: (cur - 2) * singleNavWidth
    })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
    console.log(event)
    console.log(this.data.navData)
    console.log(event.currentTarget.dataset.current)
    console.log(this.data.navData[event.currentTarget.dataset.current])
    this.getTopCatList(this.data.navData[this.data.currentTab].jingdong_id);
  },

  switchTab(event) {
    var cur = event.detail.current;
    var singleNavWidth = this.data.windowWidth / 5;
    this.setData({
      currentTab: cur,
      navScrollLeft: (cur - 2) * singleNavWidth
    });
  }
  
});
